//
//  MBProgressHUD+NJ.m
//  NJWisdomCard
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 Weconex. All rights reserved.
//

#import "MBProgressHUD+NJ.h"

@implementation MBProgressHUD (NJ)

+ (void)showError:(NSString *)error  delay:(NSTimeInterval)delay
{
    if ([error isEqualToString:@"null"]) {
        return;
    }
    [self showError:error toView:nil delay:delay];
}

/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view  delay:(NSTimeInterval)delay{
    [self show:error icon:@"error.png" view:view delay:delay];
}


+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view delay:(NSTimeInterval)delay
{
    
    if(@available(iOS 11.0, *)) {
        if (view == nil)
        {
            view = [[UIApplication sharedApplication] keyWindow];
        }
    }else{
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud..detailsLabelText = text;
//    hud.detailsLabelFont =[UIFont systemFontOfSize:14];
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    hud.contentColor = [UIColor whiteColor];
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
//    [hud hide:YES afterDelay:delay];
    [hud hideAnimated:YES afterDelay:delay];
}

/**
 *  显示信息
 *
 *  @param text 信息内容
 *  @param icon 图标
 *  @param view 显示的视图
 */
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if(@available(iOS 11.0, *)) {
        if (view == nil) {
            view = [[UIApplication sharedApplication] keyWindow];
        }
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    }else{
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    hud.contentColor = [UIColor whiteColor];
//    hud.detailsLabelText = text;
//    hud.detailsLabelFont =[UIFont systemFontOfSize:14];
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
//    [hud hide:YES afterDelay:1];
    if (text.length > 15) {
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    [hud hideAnimated:YES afterDelay:1];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error
{
    if ([error isKindOfClass:[NSString class]] && error.length) {
        [self showError:error toView:nil];
    }
}

/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

/**
 *  显示错误信息
 *
 *  @param message 信息内容
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

/**
 *  显示一些信息
 *
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    hud.contentColor = [UIColor whiteColor];
    hud.detailsLabel.text = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeIndeterminate;
    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    [self hideHUDForView:view animated:YES];
}

+ (MBProgressHUD *)showToBottomMessage:(NSString *)message toView:(UIView *)view
{
    if (![message length]) {
        return nil;
    }
    if(@available(iOS 11.0, *)) {
        if (view == nil)
        {
            view = [[UIApplication sharedApplication] keyWindow];
        }
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    } else {
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // Set the text mode to show only text.
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    hud.contentColor = [UIColor whiteColor];
//    hud.opacity = 0.8; // mbview 透明度
    hud.offset = CGPointMake(0, kSCREEN_HEIGHT /2 - 120);
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5];
    return hud;
}

+ (MBProgressHUD *)showToTopMessage:(NSString *)message toView:(UIView *)view
{
    
    if(@available(iOS 11.0, *)) {
        if (view == nil)
        {
            view = [[UIApplication sharedApplication] keyWindow];
        }
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    } else {
        if (view == nil)
        {
            view = [[UIApplication sharedApplication].windows lastObject];
        }
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // Set the text mode to show only text.
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    hud.contentColor = [UIColor whiteColor];
//    hud.opacity = 0.8; // mbview 透明度
    hud.offset = CGPointMake(0, -(kSCREEN_HEIGHT /2)+kSafeAreaInsets.top+10);
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1.5];
    return hud;
}


@end
